package org.pure.ioc;

import org.pure.ioc.annotation.Inject;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Set;

/**
 * Created by will.lin on 2015/8/17.
 */
public class IocContainer {

    private static Map<Class<?>, Object> classContainer =BeanFactory.beanMap;

    public static void init() throws IllegalAccessException {

        Set<Class<?>> classSet = classContainer.keySet();

        for (Class<?> clazz : classSet) {
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                if (field.isAnnotationPresent(Inject.class)) {
                    Inject inject = field.getAnnotation(Inject.class);
                    Class<?> impl = inject.value();
                    Object implObject = classContainer.get(impl);
                    boolean accessible = field.isAccessible();
                    field.setAccessible(true);
                    field.set(classContainer.get(clazz), implObject);
                    field.setAccessible(accessible);
                }
            }

        }
    }


}
